<?php

/**
 *    Desc
 *
 *    @author    Garbin
 *    @usage    none
 */
class StyleApp extends MemberbaseApp
{
	var $_style_mod;
    function __construct()
    {
        $this->StyleApp();
    }
    function StyleApp()
    {
        parent::__construct();
		$this->check_powers(USER_ADMIN);
		$this->_style_mod =& m('style');
    }
    function index()
    {
		$styles = $this->_style_mod->findAll();
		$this->assign('styles', $styles);
		
        $this->display('style.index.html');
		$this->_curlocal(Lang::get('style_manage'));
    }
	
	function _assign_form()
	{
		$style = array(
			'if_show' => 1,
		);
		$this->assign('style', $style);
	}
	
	function add()
	{
		if (!IS_POST) {
			$this->_assign_form();
			
	        $this->display('style.form.html');
			$this->_curlocal(Lang::get('style_manage'), 'index.php?app=style', Lang::get('add_new_style'));
		} else {
			$style_name = trim($_POST['style_name']);
			$description = trim($_POST['description']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($style_name)) {
				$this->show_warning('style_name_not_empty');
				return;
			}
			if (!$this->_style_mod->unique($style_name)) {
				$this->show_warning('style_name_is_exist');
				return;
			}
			
			$data = array(
				'style_name' => $style_name,
				'description' => $description,
				'if_show' => $if_show
			);
			
			$style_id = $this->_style_mod->add($data);
			if (!$style_id) {
				$this->_error($this->_style_mod->get_error());
				return;
			}
			$this->show_message('add_style_success', Lang::get('style'), 'index.php?app=style');
		}
	}
		
	function edit()
	{
		$style_id = intval($_GET['sid']);
		if (!$style_id) {
			$this->show_warning('style_not_exist');
			return;
		}
		$style = $this->_style_mod->get_info($style_id);
		if (!$style) {
			$this->show_warning('style_not_exist');
			return;
		}
		if (!IS_POST) {
			$this->assign('style', $style);
			
			$this->display('style.form.html');
			$this->_curlocal(Lang::get('style_manage'), 'index.php?app=style', Lang::get('edit_style'));
		} else {
			$style_name = trim($_POST['style_name']);
			$description = trim($_POST['description']);
			$if_show = intval($_POST['if_show']);
			
			if (empty($style_name)) {
				$this->show_warning('style_name_not_empty');
				return;
			}
			if (!$this->_style_mod->unique($style_name, $style_id)) {
				$this->show_warning('style_name_is_exist');
				return;
			}
			
			$data = array(
				'style_name' => $style_name,
				'description' => $description,
				'if_show' => $if_show
			);
			
			$this->_style_mod->edit($style_id, $data);
			if ($this->_style_mod->get_error()) {
				$this->_error($this->_style_mod->get_error());
				return;
			}
			$this->show_message('edit_style_success', Lang::get('style'), 'index.php?app=style');
		}
	}
	
		
	function del()
	{
		$style_id = intval($_GET['sid']);
		if (!$style_id) {
			$this->show_warning('style_not_exist');
			return;
		}
		$style = $this->_style_mod->drop("style_id={$style_id}");
		if (!$style) {
			$this->show_warning('style_not_exist');
			return;
		}

			if ($this->_style_mod->get_error()) {
				$this->_error($this->_style_mod->get_error());
				return;
			}
		$this->show_message('删除款式成功', Lang::get('style'), 'index.php?app=style');		
	}
	
	function edit_size(){
		$style_id = isset($_GET['sid'])?intval($_GET['sid']):0;
		$style_mod =& m('style');
		$_size_mode = & m('size');
		$style_info=$style_mod ->get($style_id);
		if(empty($style_info))
		{
			$this->show_warning('不存在此款式');
			return;
		}
		if(!IS_POST){

		$this->assign('style_list', $style_mod->find(array('conditions'=>'if_show=1','order'=>'style_name DESC')));
			
		$this->assign('style', $this->_style_mod->get_info($style_id));
		$style_size = $_size_mode->find(array('conditions'=>'style_id='.$style_id,'order'=>'sort_order ASC'));
		$this->assign('style_size', $style_size);
		$this->assign('style_info', $style_info);
		//print_r($style_size);
		//赋值尺寸表类型
		require(ROOT_PATH.'/app/ss.app.php');
		SsApp::_assign_sizename($this,NULL,$style_info['style_size_sn_id']);
		$ss_cq_type=LANG::get("ss_cq_type");		
	    $this->assign("curr_size_type",$ss_cq_type[$style_info['style_size_type']]);

		$this->assign("ss_cq_type_json",ecm_json_encode(lang::get("ss_cq_type")));
		
		$this->display('style.edit_size.html');
		}else{
			if (!$style_id) {
				$this->show_warning('请选择款式再保存');
				return;
			}
			$style_size_sn_id=isset($_POST['style_size_sn_id'])?intval($_POST['style_size_sn_id']):0;
			$style_mod->edit("style_id={$style_id}",array("style_size_sn_id"=>$style_size_sn_id));
	
			$_size_mode->drop("style_id=".$style_id);
			foreach($_POST['position'] as $key=>$item){
				if($_POST['position'][$key]){  //过滤掉那些没有输入部位的数据
					$size_data=array(
							"style_id"=>$style_id,
							"position"=>isset($_POST['position'][$key])?trim($_POST['position'][$key]):"",
							"w6"=>isset($_POST['w6'][$key])?floatval($_POST['w6'][$key]):"",
							"w8"=>isset($_POST['w8'][$key])?floatval($_POST['w8'][$key]):"",
							"w10"=>isset($_POST['w10'][$key])?floatval($_POST['w10'][$key]):"",
							"w12"=>isset($_POST['w12'][$key])?floatval($_POST['w12'][$key]):"",
							"w14"=>isset($_POST['w14'][$key])?floatval($_POST['w14'][$key]):"",
							"w16"=>isset($_POST['w16'][$key])?floatval($_POST['w16'][$key]):"",
							"code"=>isset($_POST['code'][$key])?floatval($_POST['code'][$key]):"",
							//"sort_order"=>isset($_POST['sort_order'][$key])?intval($_POST['sort_order'][$key]):"",
					);
					//增加排序数据
					$_POST['sort_order'][$key] && $size_data['sort_order'] = intval($_POST['sort_order'][$key]);
									if(!empty($size_data['position'])||!empty($size_data['w6'])||!empty($size_data['w8'])||!empty($size_data['w10'])||!empty($size_data['w12'])||!empty($size_data['w14'])||!empty($size_data['w16'])||!empty($size_data['code']))
				{
					foreach($size_data as $key=>$item)
					{
						if(in_array($key,array("w6","w8","w10","w12","w14","w16","code")))
						{
							if(empty($item))
							{
							unset($size_data[$key]);
							}
						}
					}
					
    			$size_ss_id = $_size_mode->add($size_data);
				}
				//	$_size_mode->add($size_data);
					//print_r($size_data);
				}
			}
			$this->show_message('更新款式尺码成功！','返回款式列表','index.php?app=style&act=edit_size&sid='.$style_id);
		}
	}
	
	function check_name()
	{
		$style_name = empty($_GET['style_name']) ? null : trim($_GET['style_name']);
		$style_id = empty($_GET['style_id']) ? 0 : intval($_GET['style_id']);
		if (!$style_name) {
            echo ecm_json_encode(false);
            return;
		}
		echo ecm_json_encode($this->_style_mod->unique($style_name, $style_id));
	}
	
}

?>
